package org.openkanban.client.ui.footer;

import org.openkanban.client.datasource.BoardContactDS;
import org.openkanban.client.ui.board.BoardComposite;
import org.openkanban.client.ui.presenter.AddContactToBoardRequestCallback;
import org.openkanban.client.ui.presenter.DeleteBoardPerformerCallback;
import org.openkanban.client.ui.presenter.PreparedRequest;
import org.openkanban.client.util.RootPanel;
import org.openkanban.client.util.StringUtils;

import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.util.EventHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.events.DropEvent;
import com.smartgwt.client.widgets.events.DropHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.tile.TileGrid;
import com.smartgwt.client.widgets.tile.TileRecord;

/**
 * GUI Element for the BoardContactTileGrid. Part of the BoardComposite so it
 * must react if board switches.
 */
public class BoardContactTileGrid extends TileGrid implements BoardComposite {

    public BoardContactTileGrid() {
	setWidth100();
	setHeight(146);
	setTileWidth(50);
	setTileHeight(58);
	setCanAcceptDrop(true);
	setPreventDuplicates(true);
	setAutoFetchData(false);
	setSmoothFade(true);
	setStyleName("boardContactTileGrid");
	setMargin(0);
	setPadding(0);
	setTileVMargin(1);
	setTileHMargin(3);
	setDragDataAction(DragDataAction.NONE);
	setCanDragTilesOut(true);
	setDropTypes("board_contact");

	addDropHandler(new DropHandler() {
	    public void onDrop(DropEvent event) {

		Long currentBoardId = RootPanel.main.getBoard().getWhiteBoardView().getBoardDto().getBoardId();

		Canvas target = EventHandler.getDragTarget();
		if (target instanceof ListGrid) {
		    ListGrid dragListGird = (ListGrid) target;
		    Record dragRecord = dragListGird.getDragData()[0];
		    String userId = dragRecord.getAttribute("userId");
		    if (StringUtils.isNumeric(userId) && currentBoardId != null && currentBoardId > 0) {
			PreparedRequest prepReq = new PreparedRequest("board", currentBoardId + "/contact/add/" + userId);
			prepReq.sendAsyncRequest(new AddContactToBoardRequestCallback());
		    }
		}
	    }
	});

	addDoubleClickHandler(new DoubleClickHandler() {

	    @Override
	    public void onDoubleClick(DoubleClickEvent event) {
		BoardContactTileGrid bctg = (BoardContactTileGrid) event.getSource();
		TileRecord tr = bctg.getSelectedRecord();
		if (tr != null) {
		    String vorname = tr.getAttribute("vorname");
		    String nachname = tr.getAttribute("nachname");
		    SC.confirm("Do you want to remove " + vorname + " " + nachname + " from Board?",
			    new DeleteBoardPerformerCallback(event.getSource()));
		}
	    }
	});

	RootPanel.boardComponent.addComposite(this);
    }

    @Override
    public void switchBoard(Long boardId) {
	if (getDataSource() != null) {
	    getDataSource().destroy();
	    BoardContactDS.setInstanceNull();
	}
	setDataSource(BoardContactDS.getInstance(boardId.toString()));
	fetchData();
	filterData();
	redraw();
    }
}
